package controller;

import java.net.URL;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ResourceBundle;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import application.Main;
import studysystem.db.MySQLConnector;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class LoginController implements Initializable{

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
	
	}

	@FXML
	private AnchorPane loginanchorpane;
	@FXML
	private AnchorPane signinanchorpane;
	@FXML
	private Button btnLogin;
	@FXML
	private Button btnSignin;
	@FXML
	private Label txterror;
	@FXML
	private TextField fieldlogin;
	@FXML
	private PasswordField fieldpassword;
	@FXML
	private TextField signinlogintextfield;
	@FXML
	private PasswordField signinpasswordtextfield;
	@FXML
	private PasswordField confirmpassfield;
	@FXML
	private Button submitsigninbtn;
	@FXML
	private Label regerrmessage;
	@FXML
	private Stage stage;
	
	public void loginaction(ActionEvent event)
	{
		String userId="0";
		 try {
             MySQLConnector conn = new MySQLConnector();
             ResultSet users = conn.execSQLQuery("SELECT * FROM USER");
            
             while (users.next()) {
            	 if ((users.getString("login").equals(fieldlogin.getText()))&&(users.getString("password").equals(fieldpassword.getText())))
            	 {userId=users.getString("id");
            	 Main.id=users.getString("id");
            	 Main.name=users.getString("login");
            	 }
             }
             if (userId=="0")
             {txterror.setVisible(true);} else 
             {
            	 
            	 Parent root = FXMLLoader.load(getClass().getResource("/application/MainScreen.fxml"));
     			Scene scene = new Scene(root); 
     			//stage= new Stage();
     			Stage stage = (Stage) btnLogin.getScene().getWindow();
    			stage.setScene(scene);
    			stage.show();
    			
            	 }
     } catch (Exception e) {
             e.printStackTrace();
     }
	} 
	
	
		 public void  signinaction(ActionEvent event)
	{
			 fieldlogin.clear();
			 fieldpassword.clear();
			 
			 txterror.setVisible(false);
			loginanchorpane.setVisible(false);
			signinanchorpane.setVisible(true);
	}
		 
	
		 public void submitSignIn (ActionEvent event)
			{
			 if (!signinpasswordtextfield.getText().equals(confirmpassfield.getText())){regerrmessage.setVisible(true);;}
			 else {
				 
				 
				 try {
		             MySQLConnector conn = new MySQLConnector();
		             java.util.Date date= new java.util.Date();     
		             DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		      	   Date dat = new Date();
		             conn.execSQLUpdate
		             ("INSERT INTO USER (login, password, created_at) VALUES"
		         + "('"+signinlogintextfield.getText()+"','"+signinpasswordtextfield.getText()+"','"+dateFormat.format(dat)+"')");   
		             ResultSet a=conn.execSQLQuery("SELECT ID FROM USER WHERE LOGIN='"+signinlogintextfield.getText()+"'");
		             
		             while (a.next()){
		            	 conn.execSQLUpdate("INSERT INTO Available_chapters(chapter_id, user_id) values ('1','"+a.getString("id")+"')");}
		             signinanchorpane.setVisible(false);
		             loginanchorpane.setVisible(true);
		             
				 } catch (Exception e) {
		             e.printStackTrace();
		     } 
			 }
			}
}
